package com.studentSystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.studentSystem.mapper.ClassesMapper;
import com.studentSystem.mapper.TeacherMapper;
import com.studentSystem.pojo.Classes;
import com.studentSystem.pojo.Teacher;
import com.studentSystem.service.ClassService;
import com.studentSystem.utils.PageBeanUtil;
import com.studentSystem.utils.SnowflakeIdWorkerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public List<Class> list() {
        return classesMapper.list();
    }

    @Override
    public PageBeanUtil<Class> listAll(Integer pageNum, Integer pageSize, String grade, Long id, String advisor) {
        int defaultPageNum = 1;
        int defaultPageSize = 5;

        if (pageNum == null || pageNum <= 0) {
            pageNum = defaultPageNum;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = defaultPageSize;
        }
        PageHelper.startPage(pageNum, pageSize);

        List<Class> classes;
        classes = classesMapper.listAll(grade, id, advisor);

        PageInfo<Class> pageInfo = new PageInfo<>(classes);

        PageBeanUtil<Class> pageBeanUtil = new PageBeanUtil<>();
        pageBeanUtil.setTotal(pageInfo.getTotal());
        pageBeanUtil.setItems(pageInfo.getList());

        return pageBeanUtil;
    }

    @Override
    public void addClass(Classes classes) {
        Long id = new SnowflakeIdWorkerUtil(0, 0).nextId();
        classes.setId(id);
        classesMapper.addClass(classes);
    }

    @Override
    public void deleteClass(Long id) {
        classesMapper.delete(id);
    }

    @Override
    public void updateClass(Classes classes) {
        classesMapper.updateClass(classes);
    }

    @Override
    public List<Classes> all() {
        return classesMapper.all();
    }
}
